
const http = require("http")
const fs = require("fs")
const optfile = require("./refile")

let pro = http.createServer((req, res) => {
    // res.setHeader("Access-Control-Allow-Origin", "*");
    // res.writeHead(200, { "Content-Type": "application/json" });

    //跨域必须在头部，响应头
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.writeHead(200, {
        'Content-Type': 'text/plain'
    })
    function recall(data){  //等待readfile执行完之后回调
        response.write(data);
        response.end('ok');
    }
    fs.readFile('../page/ft.text',recall)
    // fs.readFile('../page/ft.text',function(err,data){

    //     if(err){console.log(err);}
    //     res.write(data)
    //     // console.log(data.toString());
    //     console.log("主程序结束");
    // }  )

    // res.header("Access-Control-Allow-Origin", "*");
    // res.setHeader('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    // res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
    // res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
    res.write('This is a server page');
    res.end('okay')
})

let server = pro.listen(1337, 'localhost', (err, data) => {
    if (err) throw err
    console.log(data);

    let host = server.address().address
    let port = server.address().port

    console.log("访问 %s:%s", host, port);


})